package com.todolist.server.persistence;

import com.todolist.shared.domain.Task;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.ArrayList;

public interface TaskMapper {

    Task getTask(String text);

    @Select("INSERT INTO tasks (project_id, text, priority) VALUES (#{projectId}, #{text}, #{priority})")
    void addTask(@Param("projectId") Integer projectId, @Param("text") String text, @Param("priority") Integer priority);

    @Select("UPDATE tasks SET text = #{newText} WHERE text = #{oldText}")
    void updateTaskText(@Param("oldText") String oldText, @Param("newText") String newText);

    @Select("UPDATE tasks SET completed = #{completed} WHERE text = #{text}")
    void updateTaskCompleteness(@Param("text") String text, @Param("completed") Boolean completed);

    @Select("UPDATE tasks SET priority = #{priority} WHERE text = #{text}")
    void updateTaskPriority(@Param("text") String text, @Param("priority") Integer priority);

    @Select("DELETE FROM tasks WHERE project_id = #{projectId} AND text = #{text}")
    void deleteTask(@Param("projectId") Integer projectId, @Param("text") String text);

    @Select("DELETE FROM tasks WHERE project_id = #{projectId}")
    void deleteTasks(@Param("projectId") Integer projectId);

    @Select("SELECT * FROM tasks WHERE project_id = #{projectId} ORDER BY priority")
    ArrayList<Task> getTasks(@Param("projectId") Integer projectId);

    @Select("SELECT max(priority) FROM tasks")
    Integer getMaxTaskPriority();
}
